package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.commons.PageDto;
import com.rongke.model.User;
import com.rongke.model.UserAuth;
import com.rongke.model.UserBank;
import com.rongke.service.UserAuthService;
import com.rongke.service.UserBankService;
import com.rongke.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version : Ver 1.0
 * @UserBankController
 * @Controller
 */
@RestController
@RequestMapping(value = "/api/userBank")
@Transactional
@CrossOrigin
public class UserBankController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserBankService userBankService;
    @Autowired
    private UserService userService;

    /**
     * @param userBank
     * @return 返回值JsonResp
     * @添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addUserBank(@RequestBody UserBank userBank) {
        log.debug("添加");
        userBankService.insert(userBank);
        return JsonResp.ok(userBank);
    }

    /**
     * @param userBank
     * @return 返回值JsonResp
     * @修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateUserBank(@RequestBody UserBank userBank) {
        log.debug("修改");
        userBankService.updateById(userBank);
        return JsonResp.ok(userBank);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserBank(Long id) {
        log.debug("查找");
        UserBank userBank = userBankService.selectById(id);
        return JsonResp.ok(userBank);
    }


    /**
     * @param
     * @return 返回值JsonResp
     * @当前用户绑定的银行卡
     */
    @RequestMapping(value = "/selectBankByLoginUser", method = RequestMethod.GET)
    public JsonResp selectUserBank() {
        log.debug("当前用户绑定的银行卡");
        User user = userService.findLoginUser();
        EntityWrapper<UserBank> ew = new EntityWrapper();
        ew.eq("user_id",user.getId());
        UserBank userBank = userBankService.selectOne(ew);
        return JsonResp.ok(userBank);
    }

    /**
     * @param
     * @return 返回值JsonResp
     * @根据id查找
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.GET)
    public JsonResp findByPage(Integer pageNo, Integer pageSize,String realName){
        log.debug("分页查找银行卡认证信息");

        Page page = new Page(pageNo,pageSize);
        EntityWrapper<UserBank> entityWrapper = new EntityWrapper<>();
        if (!realName.equals("")) {
            entityWrapper.like("name",realName);
        }
        Page page1 = userBankService.selectPage(page,entityWrapper);

        return JsonResp.ok(new PageDto(pageNo,pageSize,page1.getRecords(),page1.getTotal()));
    }

    /**
     * 根据user_id查询银行卡认证详情
     */
    @RequestMapping(value = "/selectByUserId",method = RequestMethod.GET)
    public JsonResp selectByUserId(String id){
      EntityWrapper<UserBank>userBankEntityWrapper=new EntityWrapper<>();
        userBankEntityWrapper.eq("user_id",id);
        UserBank userBank=userBankService.selectOne(userBankEntityWrapper);
        return  JsonResp.ok(userBank);


    }

    @Autowired
    private UserAuthService userAuthService;
    /**
     * @param
     * @return 返回值JsonResp
     * @银行卡认证（同盾）
     */
    @RequestMapping(value = "/bankAuth", method = RequestMethod.GET)
    synchronized public JsonResp bankAuth(String idNumber,String cardNumber,String accountName,String accountMobile,String bankName){
        System.out.println(accountName);
        log.debug("银行卡认证（同盾）");
        //处理银行卡号空格
        String banCard = cardNumber.replaceAll(" ","");

        User user = userService.findLoginUser();
        EntityWrapper<UserBank> ewBank = new EntityWrapper();
        ewBank.eq("user_id",user.getId());
        UserBank oldUserBank = userBankService.selectOne(ewBank);
        if(null!=oldUserBank){
            if(cardNumber.equals(oldUserBank.getBankcardno())){
                return  JsonResp.fa("修改银行卡号不能与已绑定银行卡相同！");
            }
            oldUserBank.setUserId(user.getId());
            oldUserBank.setBankcardno(banCard);
            oldUserBank.setName(accountName);
            oldUserBank.setBankPhone(accountMobile);
            oldUserBank.setIdcardno(idNumber);
            oldUserBank.setBankName(bankName);
            userBankService.updateById(oldUserBank);
        }else{
            oldUserBank = new UserBank();
            oldUserBank.setUserId(user.getId());
            oldUserBank.setBankcardno(banCard);
            oldUserBank.setName(accountName);
            oldUserBank.setBankPhone(accountMobile);
            oldUserBank.setIdcardno(idNumber);
            oldUserBank.setBankName(bankName);
            userBankService.insert(oldUserBank);
        }

        //改变认证状态
        EntityWrapper<UserAuth> ew = new EntityWrapper();
        ew.eq("user_id",user.getId());
        UserAuth userAuth = userAuthService.selectOne(ew);
        userAuth.setBankAuth(1);
        userAuthService.updateById(userAuth);
        user.setAuthStatus(1);
        userService.updateById(user);
        return JsonResp.ok("认证成功");
    }






}
